package com.car.order.aop;


import com.car.order.annotation.GoodsOrderLogAnnotation;
import com.car.order.dto.goodOrderInfo.GoodOrderInfoDto;
import com.car.order.dto.goodOrderInfo.PayOrderForm;
import com.car.order.entity.GoodOrderLog;
import com.car.order.mapper.GoodOrderLogMapper;
import com.car.order.util.GoodsOrderDescConstants;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;



@Component
@Aspect
@Slf4j
public class GoodsOrderLogAop {
    @Resource
    private GoodOrderLogMapper goodOrderLogMapper;
    @Around("@annotation(com.car.order.annotation.GoodsOrderLogAnnotation)")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        //获取注解
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        GoodsOrderLogAnnotation declaredAnnotation = signature.getMethod().getDeclaredAnnotation(GoodsOrderLogAnnotation.class);
        /* 获取方法入参 */
        Object[] args = pjp.getArgs();
        // 获取注解上的，订单操作描述
        String desc = declaredAnnotation.desc();
        GoodOrderLog goodOrderLog = new GoodOrderLog();
        Object proceed = pjp.proceed();
        goodOrderLog.setLogDesc(desc);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        goodOrderLog.setLogTime(LocalDateTime.now().format(formatter));
        //如果是用户新增订单
        if (desc.equals(GoodsOrderDescConstants.USER_CREATE)) {
            GoodOrderInfoDto proceed1 = (GoodOrderInfoDto) proceed;
            goodOrderLog.setGoodOrderId(proceed1.getGoodOrderId());
        }
        //如果是用户支付订单
        if (desc.equals(GoodsOrderDescConstants.USER_PAID)) {
            for (Object arg : args) {
                if (arg instanceof PayOrderForm) {
                    PayOrderForm form = (PayOrderForm) arg;
                    goodOrderLog.setGoodOrderId(form.getOrderId());
                }
            }
        }
        //如果是用户退款
        if (desc.equals(GoodsOrderDescConstants.USER_TUI_KUAN)) {
            for (Object arg : args) {
                if (arg instanceof String) {
                    String id = (String) arg;
                    goodOrderLog.setGoodOrderId(id);
                }
            }
        }
        //用户换货
        if (desc.equals(GoodsOrderDescConstants.USER_HUAN_HUO)) {
            String id="";
            for (Object arg : args) {
                if (arg instanceof String) {
                    id = (String) arg;
                }
            }
            String newId = (String) proceed;
            GoodOrderLog newLog = new GoodOrderLog();
            newLog.setLogTime(goodOrderLog.getLogTime());
            newLog.setLogDesc(desc + ",生成的新订单,旧订单："+id);
            goodOrderLog.setLogDesc(desc + ",新订单：" + newId);
            newLog.setGoodOrderId(newId);
            goodOrderLogMapper.insert(newLog);
        }
        goodOrderLogMapper.insert(goodOrderLog);
        return proceed;
    }

}
